package com.baitengsoft.patentapimonitor.api.service.impl;

import com.baitengsoft.patentapimonitor.api.dao.PacApplicantDao;
import com.baitengsoft.patentapimonitor.api.dao.PacPatentinfoDao;
import com.baitengsoft.patentapimonitor.api.dao.RptpatentDataAreaDao;
import com.baitengsoft.patentapimonitor.api.model.*;
import com.baitengsoft.patentapimonitor.api.model.param.WebMonitorYearsInfo;
import com.baitengsoft.patentapimonitor.api.service.CfgDictionaryService;
import com.baitengsoft.patentapimonitor.api.service.CfgPatentIndicatorService;
import com.baitengsoft.patentapimonitor.api.service.RptPatentDataAreaService;
import com.baitengsoft.patentapimonitor.api.service.WebHomeService;
import com.baitengsoft.patentapimonitor.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

/**
 * @author Chen 2018/1/22
 */
@Service
public class WebHomeServiceImpl implements WebHomeService {
    @Autowired
    private RptpatentDataAreaDao rptpatentDataAreaDao;
    @Autowired
    private PacPatentinfoDao pacPatentinfoDao;
    @Autowired
    private CfgDictionaryService cfgDictionaryService;
    @Autowired
    private PacApplicantDao pacApplicantDao;
    @Autowired
    private CfgPatentIndicatorService cfgPatentIndicatorService;
    @Autowired
    private RptPatentDataAreaService rptPatentDataAreaService;

    /**
     * 当前有效专利
     */
    @Override
    public WebBaseAnalysisInfo getCurrentYxcount(Integer platformid) {
        return rptpatentDataAreaDao.getCurrentYxcount(platformid);
    }

    /**
     * 今年新增
     */
    @Override
    public WebBaseAnalysisInfo getXzcountYear(Integer platformid) {
        Date startDate = DateUtils.getFirstDateOfThisYear();
        Date endDate = DateUtils.getEndDateOfThisYear();
        return rptpatentDataAreaDao.getXzcountYear(platformid, startDate, endDate);
    }

    /**
     * 获取今年新增授权
     */
    @Override
    public WebBaseAnalysisInfo getSqcountYear(Integer platformid) {
        Date startDate = DateUtils.getFirstDateOfThisYear();
        Date endDate = DateUtils.getEndDateOfThisYear();
        return rptpatentDataAreaDao.getSqcountYear(platformid, startDate, endDate);
    }

    /**
     * 最近七年转入
     */
    @Override
    public List<WebMonitorYearsInfo> getInByYearRange(Integer platformid) {
        List<WebMonitorYearsInfo> infos = new ArrayList<>();
        List<Integer> years = DateUtils.getNearSevenYears();
        for (Integer year : years) {
            WebMonitorYearsInfo info = new WebMonitorYearsInfo();
            Date startDate = DateUtils.getYearFirst(year);
            Date endDate = DateUtils.getYearLast(year);
            info.setYear(year);
            info.setIn(pacPatentinfoDao.getInByYear(startDate, endDate, platformid));
            info.setOut(pacPatentinfoDao.getOutByYear(startDate, endDate, platformid));
            infos.add(info);
        }
        return infos;
    }

    /**
     * 【概览】获取失效专利原因汇总
     *
     * @param platformid
     * @return
     */
    @Override
    public List<WebInvalidReasonInfo> getInvalidReasonInfo(Integer platformid) {
        List<WebInvalidReasonInfo> infos = pacPatentinfoDao.getInvalidReasonInfo(platformid);
        Map<String, String> lowStatus = cfgDictionaryService.getLowStatus();
        for (WebInvalidReasonInfo info : infos) {
            info.setReason(lowStatus.get(info.getLawState()));
        }
        return infos;
    }

    @Override
    public int getInvalidPatCount(Integer platformid) {
        return pacPatentinfoDao.getInvalidPatCount(platformid);
    }

    @Override
    public WebHomePatentSummaryInfo getPatCountSum(Integer platformid) {
        return pacPatentinfoDao.getPatCountSum(platformid);
    }

    /**
     * 权力人分析 1.当前有效量，2.专利总量"
     */
    @Override
    public List<WebApplicantAnalysisInfo> getApplicantAnalysis(Integer platformid, Integer dataTypeValue) {
        Map<String, String> applyPersonType = cfgDictionaryService.getApplyPersonType();
        List<WebApplicantAnalysisInfo> infos = pacApplicantDao.getApplicantAnalysis(platformid, dataTypeValue);
        for (WebApplicantAnalysisInfo info : infos) {
            String type = String.valueOf(info.getType());
            info.setApplyPersonType(applyPersonType.get(type));
            applyPersonType.remove(type);
        }
        for (Map.Entry<String, String> entry : applyPersonType.entrySet()) {
            WebApplicantAnalysisInfo info = new WebApplicantAnalysisInfo();
            info.setType(Integer.parseInt(entry.getKey()));
            info.setApplyPersonType(entry.getValue());
            info.setApplyPersonCount(0);
            info.setApplyPersonPatCount(0);
            infos.add(info);
        }
        return infos;
    }

    /**
     * 万人有效发明指标分析【有效发明量】，【区域常住人口】，【万人有效发明专利拥有量】
     */
    @Override
    public WebIndicatorInfo getHomeIndicatorInfo(Integer platformid) {
        //有效发明量
        WebBaseAnalysisInfo info = getCurrentYxcount(platformid);
        Integer fmcount = info.getFmcount();
        //获取人口指标[今年]
        BigDecimal population = cfgPatentIndicatorService.getPopulationByPlatAndYear(platformid, DateUtils.getCurrentYear());
        WebIndicatorInfo result = new WebIndicatorInfo();
        if (fmcount != null) {
            result.setYxfmcount(fmcount);
        } else {
            result.setYxfmcount(0);
        }
        if (population != null) {
            result.setPopulation(population);
        } else {
            result.setPopulation(BigDecimal.ZERO);
        }
        result.setYxfmWRCount(new BigDecimal(0));
        if (population == null || population.compareTo(BigDecimal.ZERO) == 0) {
            result.setYxfmWRCount(BigDecimal.ZERO);
        } else {
            BigDecimal decimal = new BigDecimal(info.getFmcount());
            result.setYxfmWRCount(decimal.divide(population, 2, RoundingMode.HALF_UP));
        }
        return result;
    }

    @Override
    public WebStatisticsInfo getIndexStatisticsInfo(Integer platformId) {
        WebStatisticsInfo info = rptpatentDataAreaDao.getIndexStatisticsInfo(platformId);
        BigDecimal population = cfgPatentIndicatorService.getPopulationByPlatAndYear(platformId, DateUtils.getCurrentYear());
        info.setPopulation(population);
        if (!population.equals(BigDecimal.ZERO)) {
            // 万人发明专利拥有量
            info.setWrfmCount(new BigDecimal(info.getYxfmCount()).divide(population, 2, RoundingMode.HALF_UP));
            // 万人专利授权指数
            info.setWrzlsqCount(new BigDecimal(info.getFmzlsqCount() + info.getSyzlsqCount() + info.getWgzlsqCount()).divide(population, 2, RoundingMode.HALF_UP));
            // 万人发明专利授权指数
            info.setWrfmzlsqCount(new BigDecimal(info.getFmzlsqCount()).divide(population, 2, RoundingMode.HALF_UP));
        }
        return info;

    }

    /**
     * 首页月份信息
     */
    @Override
    public List<WebIndicatorMonthInfo> getIndicatorByMonth(Integer platformid) {
        //获取人口指标[今年]
        BigDecimal population = cfgPatentIndicatorService.getPopulationByPlatAndYear(platformid, DateUtils.getCurrentYear());
        List<WebIndicatorMonthInfo> infos = new ArrayList<>();
        //根据月份获取有效发明量
        for (int i = 1; i < 13; i++) {
            WebIndicatorMonthInfo info = new WebIndicatorMonthInfo();
            Integer currentYear = DateUtils.getCurrentYear();
            Integer dateYM = currentYear * 100 + i;
            info.setMonth(i + "月累计");//月份
            Integer yxfmcount = rptPatentDataAreaService.getYXFMCountByDateYM(platformid, dateYM);
            info.setPopulation(population);//人口
            info.setYxfmcount(yxfmcount);//有效发明量
            if (info.getPopulation() == null || info.getYxfmcount() == null || info.getPopulation().equals(BigDecimal.ZERO)) {
                info.setYxfmWRCount(BigDecimal.ZERO);
                info.setYxfmcount(0);
            } else {
                BigDecimal decimal = new BigDecimal(yxfmcount);
                if (population == null || population.compareTo(BigDecimal.ZERO) == 0) {
                    info.setYxfmWRCount(BigDecimal.ZERO);
                } else {
                    info.setYxfmWRCount(decimal.divide(population, 2, RoundingMode.HALF_UP));//万人有效发明量
                }
            }
            infos.add(info);
        }
        return infos;
    }

    @Override
    public List<WebStatisticsTotal> getIndexStatisticsTotal(Integer platformid) {
        List<WebStatisticsTotal> totals = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("platformid", platformid);
        totals.add(getWebStatisticsTotal("所有专利", map));
        map.put("isValid", true);
        WebStatisticsTotal webStatisticsValidTotal = getWebStatisticsTotal("当前有效专利", map);
        // 这里需要发明授权的专利类型
        webStatisticsValidTotal.setFmCount(webStatisticsValidTotal.getFmgkCount());
        totals.add(webStatisticsValidTotal);
        map.remove("isValid");
//        map.put("isPublic", true);
//        totals.add(getWebStatisticsTotal("有效专利",map));
//        result.put("public", rptpatentDataAreaDao.getStaticTotal(map));
        //   map.remove("isPublic");
        map.put("isAuth", true);
        WebStatisticsTotal authTotal =getWebStatisticsTotal("今年新增授权", map);
        authTotal.setFmCount(authTotal.getFmgkCount());
        totals.add(authTotal);
        return totals;
    }

    private WebStatisticsTotal getWebStatisticsTotal(String name, Map<String, Object> map) {
        WebStatisticsTotal result = rptpatentDataAreaDao.getStaticTotal(map);
        result.setName(name);
        return result;
    }


}
